<!doctype html>
<html>
<head>
<title>The networkState of a media element that has assigned a MediaStream
  should be NETWORK_LOADING if the stream is active and NETWORK_IDLE if the
  stream is inactive.
</title>
</head>
<body>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
var t = async_test("Tests that the networkState of a video element playing a " +
                   "MediaStream is updated correctly.");
t.step(function() {
  navigator.mediaDevices.getUserMedia({audio:true, video: true})
    .then(t.step_func(stream => {
      var video = document.createElement('video');
      assert_equals(video.networkState, HTMLMediaElement.NETWORK_EMPTY);
      video.srcObject = stream;
      setTimeout(()=>{
        assert_equals(video.networkState, HTMLMediaElement.NETWORK_LOADING);
        stream.getTracks().forEach(track => track.stop());
        assert_equals(video.networkState, HTMLMediaElement.NETWORK_IDLE);

        // Removing and re-adding the stopped audio track does not change the
        // network state.
        var track = stream.getAudioTracks()[0];
        stream.removeTrack(track);
        assert_equals(video.networkState, HTMLMediaElement.NETWORK_IDLE);
        stream.addTrack(track);
        assert_equals(video.networkState, HTMLMediaElement.NETWORK_IDLE);

        // Removing and re-adding the stopped tracks does not change the
        // network state.
        var track = stream.getVideoTracks()[0];
        stream.removeTrack(track);
        assert_equals(video.networkState, HTMLMediaElement.NETWORK_IDLE);
        stream.addTrack(track);
        assert_equals(video.networkState, HTMLMediaElement.NETWORK_IDLE);

        var track = stream.getAudioTracks()[0];
        stream.removeTrack(track);
        assert_equals(video.networkState, HTMLMediaElement.NETWORK_IDLE);
        stream.addTrack(track);
        assert_equals(video.networkState, HTMLMediaElement.NETWORK_IDLE);

        // Replacing the existing tracks with new active tracks does change the
        // network state.
        navigator.mediaDevices.getUserMedia({audio:true, video:true}).then(
          t.step_func(stream2 => {
            stream.removeTrack(stream.getAudioTracks()[0]);
            stream.addTrack(stream2.getAudioTracks()[0]);
            assert_equals(video.networkState, HTMLMediaElement.NETWORK_LOADING);

            stream.removeTrack(stream.getAudioTracks()[0]);
            assert_equals(video.networkState, HTMLMediaElement.NETWORK_IDLE);

            stream.removeTrack(stream.getVideoTracks()[0]);
            stream.addTrack(stream2.getVideoTracks()[0]);
            assert_equals(video.networkState, HTMLMediaElement.NETWORK_LOADING);

            stream.removeTrack(stream.getVideoTracks()[0]);
            assert_equals(video.networkState, HTMLMediaElement.NETWORK_IDLE);

            t.done();
          }), t.unreached_func);
      });
    }), t.unreached_func);
});
</script>
</body>
</html>
